package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.mapper.EntTaskSummaryMapper;
import com.prd.model.EntTaskSummary;
import com.prd.model.EntTaskSummaryFile;
import com.prd.model.LoginUser;
import com.prd.service.EntTaskSummaryFileService;
import com.prd.service.EntTaskSummaryService;
import com.prd.service.UserService;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EntTaskSummaryServiceImpl extends ServiceImpl<EntTaskSummaryMapper, EntTaskSummary> implements EntTaskSummaryService {

    @Autowired
    private EntTaskSummaryMapper entTaskSummaryMapper;

    @Autowired
    private EntTaskSummaryFileService entTaskSummaryFileService;

    @Autowired
    private UserService userService;

    @Override
    public int insertInfo(EntTaskSummary entTaskSummary) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entTaskSummary);
        entTaskSummaryFileService.saveBatch(entTaskSummary.getListFile());
        return entTaskSummaryMapper.insert(entTaskSummary);
    }

    @Override
    public int updateInfo(EntTaskSummary entTaskSummary) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entTaskSummary);
        return entTaskSummaryMapper.updateById(entTaskSummary);
    }

    @Override
    public List<EntTaskSummary> listInfo(EntTaskSummary entTaskSummary) {
        LambdaQueryWrapper<EntTaskSummary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(entTaskSummary);
        List<EntTaskSummary> list = entTaskSummaryMapper.selectList(queryWrapper);
        for (EntTaskSummary summary1 : list) {
            summary1.setInitiator(userService.listName(Long.parseLong(summary1.getInitiator())).getMerge());
            summary1.setParticipant(userService.listName(Long.parseLong(summary1.getParticipant())).getMerge());
            summary1.setCompile(userService.listName(Long.parseLong(summary1.getCompile())).getMerge());
            summary1.setMake(userService.listName(Long.parseLong(summary1.getMake())).getMerge());
        }
        return list;
    }

    @Override
    public EntTaskSummary details(Long id) {
        EntTaskSummary entTaskSummary = entTaskSummaryMapper.selectById(id);
        LambdaQueryWrapper<EntTaskSummaryFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntTaskSummaryFile::getInterviewId, id);
        entTaskSummary.setListFile(entTaskSummaryFileService.list(queryWrapper));
        return entTaskSummary;
    }
}
